﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Enterprise.Infrastructure;
using Enterprise.Data.Article;
using Enterprise.Model.Article;
namespace Enterprise.Service.Article
{
    /// <summary>
    /// 通知公告服务
    /// </summary>
    public class ArticleService
    {
        /// <summary>
        /// 通知公告内容显示
        /// </summary>
        /// <param name="articleId"></param>
        /// <returns></returns>
        public ArticleEntity ArticleDisp(int articleId)
        {
            ArticleData data = new ArticleData();
            return data.ArticleDisp(articleId);
        }

        /// <summary>
        ///  通知公告数据列表
        /// </summary>
        /// <returns></returns>
        public List<ArticleEntity> ArticleList(int moduleId)
        {
            ArticleData data = new ArticleData();
            return data.ArticleList(moduleId);
        }

        /// <summary>
        /// 判断用户对当前通知公告是否有签收权限
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool ArticleQianshouCheck(ArticleEntity entity)
        {
            bool rbool = false;
            if (string.IsNullOrEmpty(entity.ARecevieUser))
            {
                rbool = true;
            }
            else
            {
                string[] arr = entity.ARecevieUser.Split(',');
                bool _rbool = arr.Contains(Utility.Get_UserId.ToString());
                if (_rbool || entity.ACreateUser == Utility.Get_UserId)
                {
                    rbool = true;
                }
            }
            return rbool;
        }

        /// <summary>
        ///  按签收范围进行筛选可查看的通知公告
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<ArticleEntity> ArticleQianshouList(List<ArticleEntity> list)
        {
            //加入查看范围控制
            //签收范围以及发布者则可查看
            List<ArticleEntity> reList = new List<ArticleEntity>();
            foreach (ArticleEntity model in list)
            {
                bool rbool = ArticleQianshouCheck(model);
                if (rbool)
                {
                    reList.Add(model);
                }
            }
            return reList;
        }

        /// <summary>
        /// 通知公告查询
        /// </summary>
        /// <param name="sType">查询类型</param>
        /// <param name="skey">关键字</param>
        /// <returns></returns>
        public List<ArticleEntity> ArticleSearchList(string sType,string skey)
        {
            string cmd = "Select * from ARTICLE_INFO where " + sType + " like '%" + skey + "%'";
            ArticleData data = new ArticleData();
            return data.ArticleList(cmd);
        }

        /// <summary>
        /// 通知公告 添加、修改、删除
        /// </summary>
        /// <param name="entity">组织机构实体</param>
        public void ArticleExecute(System.Web.UI.Page page,ArticleEntity entity)
        {
            try
            {
                string cmd = string.Empty;
                switch (entity.DB_Option_Action)
                {
                    case "Insert":
                        cmd = "Insert into ARTICLE_INFO(ARTICLEID,AMODULEID,ADEPARTMENT,AUSER,ATYPE,ATITLE,ATITLERU,ACONTENT,ACONTENTRU,ARELEASETIME,AINVALIDTIME,ARECEVIEUSER,ACREATEUSER,ACREATETIME) values(SEQ_ARTICLE_INFO.nextval," + entity.AModuleId + "," + entity.ADepartMent + "," + entity.AUser + ",'" + entity.AType + "','" + entity.ATitle + "','" + entity.ATitleRu + "',:ACONTENT,:ACONTENTRU,to_date('" + entity.AReleaseTime + "','YYYY-MM-DD HH24:MI:SS'),to_date('" + entity.AInvalidTime + "','YYYY-MM-DD HH24:MI:SS'),'" + entity.ARecevieUser + "'," + entity.ACreateUser + ",to_date('" + entity.ACreateTime + "','YYYY-MM-DD HH24:MI:SS'))";
                        break;
                    case "Update":
                        cmd = "Update ARTICLE_INFO set ATYPE='" + entity.AType + "',ATITLE='" + entity.ATitle + "',ATITLERU='" + entity.ATitleRu + "',ACONTENT=:AContent,ACONTENTRU=:AContentRu,ARELEASETIME=to_date('" + entity.AReleaseTime + "','YYYY-MM-DD HH24:MI:SS'),AINVALIDTIME=to_date('" + entity.AInvalidTime + "','YYYY-MM-DD HH24:MI:SS'),ARECEVIEUSER='" + entity.ARecevieUser + "' where ARTICLEID=" + entity.ArticleId;
                        break;
                    case "Delete":
                        cmd = "Delete from ARTICLE_INFO where ARTICLEID=" + entity.ArticleId;
                        break;
                }
                if (!string.IsNullOrEmpty(cmd))
                {
                    if (entity.DB_Option_Action == "Delete")
                    {
                        ArticleExecuteBySql(cmd);
                    }
                    else
                    {
                        ArticleExecuteBySqlCommand(cmd, "ACONTENT", entity.AContent, "ACONTENTRU", entity.AContentRu);
                    }
                }
            }
            catch (Exception ex)
            {
                Utility.ShowMsg(page, ex.Message);
            }
        }

        /// <summary>
        /// 通知公告操作
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void ArticleExecuteBySql(string cmd)
        {
            ArticleData data = new ArticleData();
            data.ArticleExecute(cmd);
        }

        /// <summary>
        /// 通知公告操作
        /// </summary>
        /// <param name="cmd">sql语句</param>
        public void ArticleExecuteBySqlCommand(string cmd,string item1,object item1Value,string item2,object item2Value)
        {
            ArticleData data = new ArticleData();
            data.ArticleExecute(cmd, item1, item1Value, item2, item2Value);
        }

    }
}
